/**
 *====================================================
 * 文件名称: FinanceAuditServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.FinanceAuditEntity;
import com.yuexunit.finance.project.entity.lexicon.FinanceAuditEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
 * @ClassName: FinanceAuditServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(FinanceAuditService.class)
public class FinanceAuditServiceImpl implements FinanceAuditService {

	@Override
	public Long createFinanceAuditEntity(FinanceAuditEntity entity) {
		if (entity.getBail() == null) {
			entity.setBail(BigDecimal.ZERO);
		}
		entity.setAuditNumber(SettingConstants.PRE_NUMBER_FINANCE_AUDIT + RandomUtil.generateTimeNumber());
		PersistUtil.insert(entity);
		return entity.getFinanceAuditId();
	}

	@Override
	public FinanceAuditEntity inquireLatestFinanceAudit(Long financeId) {
		List<FinanceAuditEntity> list = inquireFinanceAuditList(financeId);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public FinanceAuditEntity inquireFinanceAudit(Long financeAuditId) {
		return PersistUtil.get(FinanceAuditEntity.class, financeAuditId);
	}

	@Override
	public List<FinanceAuditEntity> inquireFinanceAuditList(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceAuditEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		criteria.addOrder(Order.desc("financeAuditId"));
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void updateFinanceAuditEntity(FinanceAuditEntity financeAuditEntity) {
		PersistUtil.update(financeAuditEntity);
	}

	@Override
	public List<FinanceAuditEntity> inquireFinanceAuditByState(
			FinanceAuditEnum financeAuditState) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceAuditEntity.class);
		criteria.add(Restrictions.eq("financeAuditState", financeAuditState));
		criteria.addOrder(Order.desc("financeAuditId"));
		return PersistUtil.get(criteria);
	}

	@Override
	public void deleteFinanceAuditEntity(FinanceAuditEntity financeAuditEntity) {
		PersistUtil.delete(financeAuditEntity);
	}
}
